package Class005;

public class BSQuestions {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,5};
        System.out.println(findPeak(arr));
    }

    //题目：任意相邻的数是不相等的。请寻找到一处峰值
    //峰值：num[i - 1] < num[i] && num[i] > num[i + 1]
    //默认数组的边界是无穷小的
    public static int findPeak(int[] arr) {
        if(arr == null) return -1;
        int N = arr.length;
        if(N < 2) return 0;
        if(arr[0] > arr[1]) return 0;
        if(arr[N - 1] > arr[N - 2]) return N - 1;
        int L = 1, R = N - 2, mid;
        //现在一定是左上升，右下降
        while(L <= R) {
            mid = L + ((R - L) >> 1);
            if(arr[mid - 1] > arr[mid]){
                R = mid - 1;
            }else if(arr[mid] < arr[mid + 1]){
                L = mid + 1;
            }else {
                return mid;
            }
        }
        return -1;
    }
}
